﻿using NewRen.Log4net;
using NewRen.Models;
using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NewRen.Areas.Admin.Controllers
{
    public class NewsController : BaseController
    {
        public OpenDB opendb = new OpenDB();
        // GET: Admin/News
        public ActionResult Index(int page=1)
        {
            return View(db.News.OrderByDescending(n => n.DateTime).ToPagedList(page, 20));
        }

        /// <summary>
        /// 删除新闻
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(int id)
        {
            var news = new News();
            try
            {
                news = db.News.Find(id);
                db.News.Remove(news);
                db.SaveChanges();
                return Content("ok");
            }
            catch(Exception ex)
            {
                log.Error(new LogContent("删除新闻失败", Helpers.HttpHelper.GetIPAddress()), ex);
                return Content("err");
            }
        }

        /// <summary>
        /// 新闻显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Show(int id)
        {
            var news = new News();
            news = db.News.Find(id);
            return View(news);
        }


        [HttpGet]
        public ActionResult Import(int page=1)
        {
            return View(opendb.News.OrderByDescending(n=>n.DateTime).ToPagedList(page, 20));
        }

        [HttpGet]
        public ActionResult SpiderShow(int id)
        {
            var news = new SpiderNews();
            news = opendb.News.Find(id);
            return View(news);
        }

        /// <summary>
        /// 删除抓取新闻
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SpiderDelete(int id)
        {
            var news = new SpiderNews();
            try
            {
                news = opendb.News.Find(id);
                opendb.News.Remove(news);
                opendb.SaveChanges();
                return Content("ok");
            }
            catch(Exception ex)
            {
                log.Error(new LogContent("删除抓取新闻失败", Helpers.HttpHelper.GetIPAddress()), ex);
                return Content("err");
            }
        }

        /// <summary>
        ///  入库
        /// </summary>
        /// <param name="Status"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult SpiderNewsImport(int id)
        {
            var news = new SpiderNews();
            try
            {
                news = opendb.News.Find(id);
                news.Status = SpiderNewsStatus.已收录;

                News _news = new News();
                _news.Title = news.Title;
                _news.Content = news.Content;
                _news.DateTime = DateTime.Now;
                _news.Category = news.Category;
                _news.Browses = 0;
                _news.Area = news.Area;
                _news.Source = news.Source;
                _news.Url = news.URL;
                _news.Summary = news.Summary;
                db.News.Add(_news);
                opendb.SaveChanges();
                db.SaveChanges();
            }
            catch(Exception ex)
            {
                log.Error(new LogContent("修改抓取新闻状态失败", Helpers.HttpHelper.GetIPAddress()), ex);
            }
            return Redirect("/Admin/News/SpiderShow/"+id);
        }
    }
}